package com.adjy.pathbuilders.screens;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.ImageButton;
import com.adjy.pathbuilders.objects.button.TextImageButton;
import com.adjy.pathbuilders.objects.button.listeners.ChangeGameStateButtonListener;
import com.adjy.pathbuilders.objects.button.listeners.QuitGameButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.state.Game;
import com.adjy.pathbuilders.state.GameState;

public class MainMenu implements GameScreen {
	private Game game;
	private List<Button> buttons;
	private AnimationFactory anFactory;
	
	public MainMenu(AnimationFactory factory, Game game) {
		this.anFactory = factory;
		this.game = game;
		buttons = new ArrayList<Button>();
	}
	
	private void createButtons() {
		
		buttons.add(new TextImageButton(700, 120, 
				new ChangeGameStateButtonListener(game, GameState.FACEBOOK_CONNECT),
				new WeakReferenceBitmap(anFactory, "selected"),
				new WeakReferenceBitmap(anFactory, "unselected"), "F"));
		
		buttons.add(new TextImageButton(
				200,
				180,
				new ChangeGameStateButtonListener(game, GameState.LEVELS_MENU),
				new WeakReferenceBitmap(anFactory, "menu_button_selected"),
						new WeakReferenceBitmap(anFactory, "menu_button_unselected"),
						game.getContext().getString(R.string.play)));
		
		buttons.add(new TextImageButton(
				200,
				280,
				new ChangeGameStateButtonListener(game, GameState.TUTORIAL),
				new WeakReferenceBitmap(anFactory, "menu_button_selected"),
						new WeakReferenceBitmap(anFactory, "menu_button_unselected"),
						game.getContext().getString(R.string.tutorial)));
		
		buttons.add(new TextImageButton(
				200,
				380,
				new QuitGameButtonListener(game),
				new WeakReferenceBitmap(anFactory, "menu_button_selected"),
						new WeakReferenceBitmap(anFactory, "menu_button_unselected"),
						game.getContext().getString(R.string.quit)));
		
		buttons.add(new ImageButton(
				700,
				20,
				new ChangeGameStateButtonListener(game, GameState.ABOUT),
				new WeakReferenceBitmap(anFactory, "about_selected"),
						new WeakReferenceBitmap(anFactory, "about_unselected")));

		buttons.add(new ImageButton(
				20,
				20,
				new ChangeGameStateButtonListener(game, GameState.OPTIONS_MENU),
				new WeakReferenceBitmap(anFactory, "settings_selected"),
				new WeakReferenceBitmap(anFactory, "settings_unselected")));
	}
	
	public void input(MotionEvent ev){
		
		for (Button b : buttons) {
			b.input(ev);
		}
	}

	@Override
	public void load() {
		anFactory.loadImages("logo", "main_menu_bg", "menu_button_selected",
				"menu_button_unselected", "about_selected", "about_unselected",
				"settings_selected", "settings_unselected", "selected", "unselected");
		createButtons();
		
	}

	@Override
	public void release() {
		anFactory.releaseImages("logo", "main_menu_bg", "menu_button_selected",
				"menu_button_unselected", "about_selected", "about_unselected",
				"settings_selected", "settings_unselected", "selected", "unselected");
		buttons.clear();
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {		
		mcanvas.drawBackground(anFactory.getImage("main_menu_bg"));
		mcanvas.drawBitmapWithConversion(anFactory.getImage("logo"), 185, 20);
		
		for (Button b : buttons) {
			b.render(mcanvas, timeElapsed);
		}
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub
		
	}
}